/**
 * Program   :   ParameterUtils.java
 * Author    :   shadow guo
 * Create    :   2013-7-3 上午9:21:00
 *
 * Copyright 2008 by Tongfu Information Technology Co. Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of Tongfu Information Technology Co. Ltd. ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with Tongfu Information Technology Co. Ltd.
 * 
 */
package com.tongfusoft.web.utils;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.tongfusoft.web.core.Constant;
import com.tongfusoft.web.core.data.DictionaryData;
import com.tongfusoft.web.core.data.SystemParamData;

public class ParameterUtils {
	/**
	 * 从Application中获取默认的项目ID
	 * @return
	 */
	public static String getDefaultProjectId(){
		return (String)ServletActionContext.getServletContext().getAttribute(Constant.DEFAULTPROJECT);
	}
	
	/**
	 * 从session中获取当前用户的项目ID
	 * @return
	 */
	public static String getCurrProjectId(){
		if(StringUtils.isNotEmpty((String)ServletActionContext.getServletContext().getAttribute(Constant.CURRPROJECT))){
			return (String)ServletActionContext.getServletContext().getAttribute(Constant.CURRPROJECT);
		}else{
			return getDefaultProjectId();
		}
		
	}
	
	/**
	 * 获取系统参数的值
	 * @param sysParams
	 * @param code
	 * @return
	 */
	public static SystemParamData findSystemParamByCode(Map<String,Map<String,SystemParamData>> sysParams,String code ){
		return  findSystemParamByCode(sysParams,code,getCurrProjectId());
	}
	
	/**
	 * 获取系统参数的值
	 * @param sysParams
	 * @param code
	 * @param project
	 * @return
	 */
	public static SystemParamData findSystemParamByCode(Map<String,Map<String,SystemParamData>> sysParams,String code ,String project){
		return sysParams.get(project).get(code);
	}
	
	/**
	 * 获取字典表列表
	 * @param dictionarys
	 * @param code
	 * @param project
	 * @return
	 */
	public static List<DictionaryData> findDictionaryByCode(Map<String,Map<String,List<DictionaryData>>> dictionarys,String code,String project){
		return dictionarys.get(project).get(code);
	}
	
	/**
	 * 获取字典表列表
	 * @param dictionarys
	 * @param code
	 * @return
	 */
	public static List<DictionaryData> findDictionaryByCode(Map<String,Map<String,List<DictionaryData>>> dictionarys,String code){
		return findDictionaryByCode(dictionarys,code,getCurrProjectId());
	}
}
